﻿using System;
using System.Linq;
using System.Net;
using System.ServiceModel.Syndication;
using System.Web.Mvc;
using System.Xml;
using Giant.Admin.Infrastructure.Cache;
using Giant.Admin.Models.Home;
using Giant.Core;
using Giant.Core.Caching;
using Giant.Core.Domain.Common;
using Giant.Core.Domain.Users;
using Giant.Services.Configuration;
using Giant.Services.Users;
using Giant.Services.Security;

namespace Giant.Admin.Controllers
{
    public partial class HomeController : BaseAdminController
    {
        #region Fields
        private readonly ISiteContext _siteContext;
        private readonly AdminAreaSettings _adminAreaSettings;
        private readonly ISettingService _settingService;
        private readonly IPermissionService _permissionService;
        private readonly IUserService _userService;
        private readonly IWorkContext _workContext;
        private readonly ICacheManager _cacheManager;

        #endregion

        #region Ctor

        public HomeController(ISiteContext siteContext,
            AdminAreaSettings adminAreaSettings, 
            ISettingService settingService,
            IPermissionService permissionService,
            IUserService userService,
            IWorkContext workContext,
            ICacheManager cacheManager)
        {
            this._siteContext = siteContext;
            this._adminAreaSettings = adminAreaSettings;
            this._settingService = settingService;
            this._permissionService = permissionService;
            this._userService = userService;
            this._workContext = workContext;
            this._cacheManager = cacheManager;
        }

        #endregion

        #region Methods

        public ActionResult Index()
        {
            var model = new DashboardModel();
            return View(model);
        }

        [ChildActionOnly]
        public ActionResult CommonStatistics()
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.ManageUsers))
                return Content("");

            

            var model = new CommonStatisticsModel();          

            model.NumberOfUsers = _userService.GetAllUsers(
                userRoleIds: new [] { _userService.GetUserRoleBySystemName(SystemUserRoleNames.Registered).Id }, 
                pageIndex: 0, 
                pageSize: 1).TotalCount;

            
            return PartialView(model);
        }

        #endregion
    }
}
